﻿#include "totaldata.h"
#include "ui_totaldata.h"

Totaldata::Totaldata(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Totaldata)
{
    ui->setupUi(this);

    // 取消窗口最大化功能
    setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);

    // 固定窗体大小
    setFixedSize(this->width(),this->height());

    initTableWidgetList();
}

Totaldata::~Totaldata()
{
    delete ui;
}

void Totaldata::on_pushButton_data_clicked()
{
    int i = 0;
    QSqlQuery query;
    query.exec("select * from commoditydatatable");

    while(query.next()) {
        QString res = query.value(0).toString();
        QString resName = query.value(1).toString();
        QString resNum = query.value(2).toString();
        QString resPrice = query.value(3).toString();
        // 计算总价
        double dPrice = resPrice.toDouble();
        int dNum = resNum.toInt();
        long long dSum = dPrice * dNum;
        QString resSum = QString("%1").arg(dSum);

        QString supplier = query.value(4).toString();
        QString director = query.value(5).toString();
        QString inTime = query.value(6).toString();
        QString outTime = query.value(7).toString();
        QString comment = query.value(8).toString();
        // 测试 res 和 resName
        // QMessageBox::information(this,"Test",res + " " + resName);

        // 将数据显示在表格中
        ui->tableWidget_data->setItem(i,0,new QTableWidgetItem(res));
        ui->tableWidget_data->setItem(i,1,new QTableWidgetItem(resName));
        ui->tableWidget_data->setItem(i,2,new QTableWidgetItem(resNum));
        ui->tableWidget_data->setItem(i,3,new QTableWidgetItem(resPrice));
        ui->tableWidget_data->setItem(i,4,new QTableWidgetItem(resSum));
        ui->tableWidget_data->setItem(i,5,new QTableWidgetItem(supplier));
        ui->tableWidget_data->setItem(i,6,new QTableWidgetItem(director));
        ui->tableWidget_data->setItem(i,7,new QTableWidgetItem(inTime));
        ui->tableWidget_data->setItem(i,8,new QTableWidgetItem(outTime));
        ui->tableWidget_data->setItem(i,9,new QTableWidgetItem(comment));
        i++;
    }
}

void Totaldata::on_pushButton_input_clicked()
{

}

void Totaldata::on_pushButton_output_clicked()
{

}

void Totaldata::on_pushButton_copy_clicked()
{

}

void Totaldata::initTableWidgetList()
{
    // id name num price supplier direator in out comment
    // 设置表格控件的行和列

    ui->tableWidget_data->setColumnCount(10);
    ui->tableWidget_data->setRowCount(200);

    // 设置字体
    ui->tableWidget_data->setFont(QFont("宋体",15));

    // 设置表格控件标题
    ui->tableWidget_data->setHorizontalHeaderLabels(QStringList() << QStringLiteral("编号") << QStringLiteral("名称") << QStringLiteral("数量") << QStringLiteral("单价") << QStringLiteral("总价")<< QStringLiteral("供应商") << QStringLiteral("负责人") << QStringLiteral("入库时间") << QStringLiteral("出库时间") << QStringLiteral("备注"));

    // 初始化每个单元格的宽度
    ui->tableWidget_data->setColumnWidth(0,80);
    ui->tableWidget_data->setColumnWidth(1,160);
    ui->tableWidget_data->setColumnWidth(2,80);
    ui->tableWidget_data->setColumnWidth(3,80);
    ui->tableWidget_data->setColumnWidth(4,80);
    ui->tableWidget_data->setColumnWidth(5,280);
    ui->tableWidget_data->setColumnWidth(6,120);
    ui->tableWidget_data->setColumnWidth(7,280);
    ui->tableWidget_data->setColumnWidth(8,300);
    ui->tableWidget_data->setColumnWidth(9,300);
}
